import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public class Admin extends JDialog {
    public Admin(JFrame frame){
        super(frame,"管理员操作界面",true);
        setBounds(720,405,300,400);
        setLayout(new FlowLayout());

        Font font = new Font("SimSun",Font.BOLD, 15);
        //---------------------------------------------------------------------------------------
        JButton passwordCtrl = new JButton("密码管理");
        passwordCtrl.setPreferredSize(new Dimension(120, 60));
        passwordCtrl.setFont(font);
        //---------------------------------------------------------------------------------------
        JButton userCtrl = new JButton("用户管理");
        userCtrl.setPreferredSize(new Dimension(120, 60));
        userCtrl.setFont(font);

        JButton exitLogIN = new JButton("退出登录");
        exitLogIN.setPreferredSize(new Dimension(120, 60));
        exitLogIN.setFont(font);
        //---------------------------------------------------------------------------------------

        add(passwordCtrl);
        add(userCtrl);
        add(exitLogIN);

        passwordCtrl.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                passwordCtrlWindows passwordCtrlWindows = new passwordCtrlWindows(Admin.this);
                passwordCtrlWindows.setVisible(true);
            }
        });

        userCtrl.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                userCtrlWindows userCtrlWindows = new userCtrlWindows(Admin.this);
                userCtrlWindows.setVisible(true);
            }
        });

        exitLogIN.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                User.nowUser = null;
                JOptionPane.showMessageDialog(null,"已经退出登录");
                dispose();
            }
        });

    }

    private static class passwordCtrlWindows extends JDialog{
        private passwordCtrlWindows(Admin frame) {
            super(frame,"管理员操作界面",true);
            setBounds(720,405,400,300);
            setLayout(new FlowLayout());

            JButton exit = new JButton("退出");

            JLabel label1 = new JLabel("请输入新管理员密码");
            JLabel label2 = new JLabel("请输入要重置密码的经理或者前台的账号（重置后密码：123456）");
            JTextField username = new JTextField(10);
            JPasswordField newPwd = new JPasswordField(10);
            JButton confirm1 = new JButton("确认");
            JButton confirm2 = new JButton("确认");

            add(label1);
            add(newPwd);
            add(confirm1);

            add(label2);
            add(username);
            add(confirm2);

            add(exit);



            confirm1.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String pwd = new String(newPwd.getPassword());
                    User.setUserMap(User.nowUser,pwd,"管理员",User.getUserMap().get(User.nowUser).get(2),User.getUserMap().get(User.nowUser).get(3));
                    Log.setLogger("操作类型：管理员修改密码，操作人："+User.nowUser);
                    JOptionPane.showMessageDialog(null,"已经修改");
                    dispose();

                }
            });

            confirm2.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    boolean flag = false;
                    for(Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()){
                        String key = entry.getKey();
                        List<String> values = User.getUserMap().get(key);
                        if( (values.get(1).equals("经理") || values.get(1).equals("前台")) && key.equals(username.getText()) ){
                            flag = true;
                            User.setUserMap(key,"123456",values.get(1),values.get(2),values.get(3));
                            Log.setLogger("操作类型：管理员重置"+values.get(1)+key+"密码"+"，操作人："+User.nowUser);
                            JOptionPane.showMessageDialog(null,"已经重置");
                            dispose();
                            break;
                        }
                    }
                    if(!flag) {
                        JOptionPane.showMessageDialog(null, "重置失败没有找到该用户");
                        dispose();
                    }
                }
            });

            exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null,"已经退出");
                    dispose();
                }
            });
        }
    }


    private static class userCtrlWindows extends JDialog{
        public userCtrlWindows(Admin admin) {
            super(admin,"管理员操作界面",true);
            setBounds(720,405,300,450);
            setLayout(new FlowLayout());
            LocalDate now = LocalDate.now();
            int i = 0;
            String [] data = new String[User.getUserMap().size()];
            JLabel label1 = new JLabel("用户名（ID）   注册时间     用户类型     手机号");
            for(Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()){
                String key = entry.getKey();
                List<String> values = User.getUserMap().get(key);
                data[i] = (key+"     "+values.get(2)+"     "+values.get(1)+"     "+values.get(3));
                i++;
            }

            JComboBox<String> box = new JComboBox<>();
            JComboBox<String> box1 = new JComboBox<>();
            JList<String> list = new JList<>(data);
            list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            JLabel label2 = new JLabel("新增用户用户名：");
            JLabel label3 = new JLabel("新增用户手机号：");
            JLabel label4 = new JLabel("新增用户类型：");
            JLabel label5 = new JLabel("请输入要修改用户的用户名：");
            JLabel label6 = new JLabel("请选择要修改的用户类型：");
            JLabel label7 = new JLabel("请输入修改的手机号：");
            JTextField checkUser = new JTextField(10);
            JTextField addUser = new JTextField(10);
            JTextField phoneNumber = new JTextField(10);
            JTextField phoneNumber1 = new JTextField(10);
            JTextField change = new JTextField(10);
            JButton check = new JButton("查询");
            JButton deleteUser = new JButton("删除");
            JButton exit = new JButton("退出");
            JButton addUserButton = new JButton("增加用户");
            box.addItem("经理");
            box.addItem("前台");
            box.addItem("管理员");
            box1.addItem("经理");
            box1.addItem("前台");
            box1.addItem("管理员");
            JButton changeUser = new JButton("修改用户信息");

            add(label1);
            add(list);
            add(checkUser);
            add(check);
            add(deleteUser);
            add(label2);
            add(addUser);
            add(label3);
            add(phoneNumber);
            add(label4);
            add(box);
            add(addUserButton);
            add(label5);
            add(change);
            add(label6);
            add(box1);
            add(label7);
            add(phoneNumber1);
            add(changeUser);
            add(exit);

            changeUser.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    boolean flag = false;
                        for (Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()) {
                            String key = entry.getKey();
                            List<String> values = User.getUserMap().get(key);
                            if (change.getText().equals(key) && values.get(1).equals("经理") || change.getText().equals(key) && values.get(1).equals("前台")) {
                                flag = true;
                                User.setUserMap(key, values.get(0), (String) box1.getSelectedItem(),values.get(2), phoneNumber1.getText());
                                JOptionPane.showMessageDialog(null, "修改成功,再次开启列表以刷新");
                                break;
                            }
                        }
                        if(!flag){
                            JOptionPane.showMessageDialog(null,"没有该用户或者该用户为管理员");
                        }
                    }

            });

            addUserButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    boolean flag = false;
                    for(Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()) {
                        String key = entry.getKey();
                        List<String> values = User.getUserMap().get(key);
                        if (key.equals(addUser.getText())||values.get(3).equals(phoneNumber.getText())) {
                            flag = true;
                            JOptionPane.showMessageDialog(null,"用户名已存在或电话号码重复");
                            break;
                        }
                    }
                    if(!flag) {
                        User.setUserMap(addUser.getText(), "123456", (String) box.getSelectedItem(), String.valueOf(now), phoneNumber.getText());
                        Log.setLogger("操作类型：管理员新增"+ box.getSelectedItem()+" "+addUser.getText() + "，操作人：" + User.nowUser);
                        JOptionPane.showMessageDialog(null, "已经添加,再次开启列表以刷新");
                    }
                }

            });

            deleteUser.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {


                    boolean flag = false;
                    for(Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()) {
                        String key1 = entry.getKey();
                        List<String> values = User.getUserMap().get(key1);
                        if (key1.equals(checkUser.getText())&&values.get(1).equals("管理员")) {
                            flag = true;
                            JOptionPane.showMessageDialog(null,"此用户为管理员，你不能删除");
                            break;
                        } else if (key1.equals(checkUser.getText())) {
                            JDialog confirm = new JDialog(admin,"确认删除",true);
                            confirm.setBounds(720,405,150,100);
                            Container c = confirm.getContentPane();
                            c.setLayout(new FlowLayout());
                            JLabel label = new JLabel("确认删除该用户？");
                            c.add(label);
                            JButton confirm1 = new JButton("确认");
                            JButton confirm2 = new JButton("取消");
                            c.add(confirm1);
                            c.add(confirm2);
                            flag = true;
                            confirm1.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    User.getUserMap().remove(checkUser.getText());
                                    Log.setLogger("操作类型：管理员删除用户" + checkUser.getText() + "，操作人：" + User.nowUser);
                                    JOptionPane.showMessageDialog(null, "已经删除,再次开启列表以刷新");
                                    confirm.dispose();
                                }
                            });
                            confirm2.addActionListener(new ActionListener() {
                                @Override
                                public void actionPerformed(ActionEvent e) {
                                    confirm.dispose();
                                }
                            });
                            confirm.setVisible(true);
                            break;
                        }
                    }
                    if(!flag){
                        JOptionPane.showMessageDialog(null,"没有找到该用户");
                    }
                }
            });

            check.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    boolean flag = false;
                    for(Map.Entry<String, List<String>> entry : User.getUserMap().entrySet()) {
                        String key1 = entry.getKey();
                        List<String> values = User.getUserMap().get(key1);
                        if (key1.equals(checkUser.getText())) {
                            flag = true;
                            JOptionPane.showMessageDialog(null, "用户名（ID）：" + key1 + "\n注册时间：" + values.get(2) + "\n用户类型：" + values.get(1) + "\n手机号：" + values.get(3));
                            break;
                        }
                    }
                    if(!flag){
                        JOptionPane.showMessageDialog(null,"没有找到该用户");
                    }
                }
            });

            exit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null,"已经退出");
                    dispose();
                }
            });
        }
    }
}